package com.lovo.common.dao.impl;

import com.lovo.common.dao.BaseDao;
import com.lovo.common.dao.ISysDictDao;
import com.lovo.common.model.SysDictBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典dao实现类
 * @author 白德柯
 *
 */
public class SysDictDaoImpl extends BaseDao implements ISysDictDao {

    @Override
    public List<SysDictBean> findByTypeId(int typeId) {
        String sql = "select * from sys_dict where type_id=?";
        this.setConnection();
        List<SysDictBean> list = new ArrayList<>();
        try {
            ps = con.prepareStatement(sql);
            ps.setObject(1,typeId);
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(fill());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public SysDictBean findById(int id) {
        String sql = "select * from sys_dict where id=?";
        this.setConnection();
        try {
            ps = con.prepareStatement(sql);
            ps.setObject(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                return fill();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private SysDictBean fill() throws SQLException {
        SysDictBean sysDictBean = new SysDictBean();
        sysDictBean.setId(rs.getInt("id"));
        sysDictBean.setTypeId(rs.getInt("type_id"));
        sysDictBean.setTypeName(rs.getString("type_name"));
        sysDictBean.setDictValue(rs.getString("dict_value"));
        return sysDictBean;
    }

    public static void main(String[] args) {
        ISysDictDao sysDictDao = new SysDictDaoImpl();
//        System.out.println(sysDictDao.findByTypeId(1002));
        System.out.println(sysDictDao.findById(1));
    }
}
